package com.aier.school.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.Classes;
import com.aier.classes.service.IClassService;
import com.aier.common.dao.ICommonDAO;
import com.aier.school.entity.AssignMessageRecord;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolTeacher;
import com.aier.school.service.ISchoolTeacherService;
import com.aier.user.common.UserFinalPageSize;
import com.aier.user.service.IUserBuyService;

@Scope("prototype")
@Component("messageManageAction")
public class MessageManageAction extends SchoolBaseAction{

    /**
     * 学校管理-短信管理
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "schoolTeacherService")
    private ISchoolTeacherService schoolTeacherService;        
    @Resource(name = "userBuyService")
    private IUserBuyService userBuyService;
    
    @Resource(name = "classService")
    private IClassService classService;
    
    private int s;                //成功:1 ,失败:0
        
    private Integer sid;        //学校id
    
    private int page;            //页数
    private int page_size;        //每页的记录数
    private int page_count;        //总页数
    private int count;            //总数
    
    
    private School school; // 学校实体
    private List<AssignMessageRecord> assignHistoryList; // 学校分配给教师的短信历史记录
    
    private List<SchoolTeacher> teacherList;    //教师列表
    private List<Classes> schoolClassList;      //学校班级列表
    private String assignList;  // 分配短信信息（格式：cid,tid,mesCount@cid,tid,mesCount）
    private Long cid; // 班级id
    
    @Override
    public String execute(){
        // 判断学校的付费方式（1按月购买2按短信条数购买）
        school = userBuyService.searchSchoolMessageCountSid(sid);
        
        schoolClassList = classService.searchClassList(sid);
        teacherList = schoolTeacherService.searchSchoolTeacherList(sid);
        
        return SUCCESS;
    }
    
    /**
     * 给班级或教师分配短信(短信条数是否够用在jsp页面判断)
     * @return
     */
    public String assignMessage() {
        s = schoolTeacherService.updateMessage(sid,assignList);
        school = userBuyService.searchSchoolMessageCountSid(sid);
        return SUCCESS;
    }
    
    
    /**
     * 查询学校或班级分配短信的历史记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String searchAssignList() {
       
        if (page <= 0) {
            page = 1;
        }
        Map<String, Object> map = schoolTeacherService.searchAssignRecord(sid,cid,page, UserFinalPageSize.PROXY_SCHOOLS_PAGESIZE);
        assignHistoryList =  (List<AssignMessageRecord>) map.get(ICommonDAO.OBJ_LIST);
        page = (Integer) map.get(ICommonDAO.PAGE);
        page_size = (Integer) map.get(ICommonDAO.PAGE_SIZE);
        page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
        count = (Integer) map.get(ICommonDAO.COUNT);
        s=1;
        return SUCCESS;
    }

    
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getPage_count() {
        return page_count;
    }

    public void setPage_count(int page_count) {
        this.page_count = page_count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getS() {
        return s;
    }

    public void setSchoolTeacherService(ISchoolTeacherService schoolTeacherService) {
        this.schoolTeacherService = schoolTeacherService;
    }

    public void setUserBuyService(IUserBuyService userBuyService) {
        this.userBuyService = userBuyService;
    }
    
    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public Integer getSid() {
        return sid;
    }

    public School getSchool() {
        return school;
    }

    public List<AssignMessageRecord> getAssignHistoryList() {
        return assignHistoryList;
    }
    
    public List<SchoolTeacher> getTeacherList() {
        return teacherList;
    }

    public List<Classes> getSchoolClassList() {
        return schoolClassList;
    }

    public void setAssignList(String assignList) {
        this.assignList = assignList;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

}
